package com.zchat.api.protocal.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.zchat.android.R;
import com.zchat.android.service_aidl_beans.TestBean;
import com.zchat.api.log.Logger;
import com.zchat.api.util.Helpers;

import android.content.Context;
import android.util.Xml;

/**
 * @author Oliverzhu
 * 2012/6/19
 *
 */
public class TestXMLProtocal implements XMLProtocal{
	private static TestXMLProtocal mTestXMLProtocal = new TestXMLProtocal();
	
	private TestXMLProtocal()
	{
		
	}
	
	public static TestXMLProtocal getInstance()
	{
		return mTestXMLProtocal;
	}

	@Override
	public Map<String, Object> parse(InputStream in)
			throws XmlPullParserException, IOException {
		
		Map<String,Object> map = null;
		
		boolean isId = false;
		
		boolean isName = false;
////		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();         
//		factory.setNamespaceAware(true);         
		TestBean tb = null;
		XmlPullParser parser = Xml.newPullParser();
				//Xml.newPullParser();
				//factory.newPullParser();
				
		parser.setInput(in,"utf-8");
		int eventCode = parser.getEventType();
		while (eventCode != XmlPullParser.END_DOCUMENT) {
			String tag = parser.getName();
			switch (eventCode) {
			case XmlPullParser.START_DOCUMENT:// 文档的开始
				map = new HashMap<String, Object>();
				tb = new TestBean();
				break;
			case XmlPullParser.START_TAG:
				if ("id".equals(tag)) {
					isId = true;
					tb.setId(Integer.valueOf(parser.nextText().toString()));
				} else if ("name".equals(tag)) {
					tb.setName(parser.nextText());
					isName = true;
				} 
				break;
//			case  XmlPullParser.TEXT:
//				if (isId) {
//					tb.setId(Integer.valueOf(parser.getText().toString()));
//				} else if (isName) {
//					tb.setName(parser.getText());
//				}
//				break;
			case XmlPullParser.END_TAG:
				if("id".equals(parser.getName()))
				{
					isId = false;
				}
				if("name".equals(parser.getName()))
				{
					isName = false;
				}
				if("volumel".equals(parser.getName()))
				{
					map.put("test", tb);
				}
				break;
			}
			eventCode = parser.next();
		}
		
		return map;
	}

}
